﻿//#define NET4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
//using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
//using System.Web.Script.Serialization;

#if NET4
using System.Runtime.Serialization.Json;
#endif


namespace Pub
{
    public class CDB
    {
        static CADO ado1 = new CADO();
        //public static DataSet ds1;

        public struct TableKey
        {
            public DataColumn dc;
            public string s_Val;
        }

        public class DBInfo
        {
            public string sSourc = "";      //服务器
            public string sDBNam = "";      //数据库
            public string sTable = "";      //表
            public string sField = "";      //字段
            public string sUser_ = "";      //用户名
            public string sPassW = "";      //密码
        }

        public static string getConStr(DBInfo dbInf)
        {
            string sDBCon = "Data Source=" + dbInf.sSourc
                + ";Initial Catalog=" + dbInf.sDBNam
                + ";Persist Security Info=True"
                + ";User ID=" + dbInf.sUser_
                + ";Password=" + dbInf.sPassW;
            return sDBCon;
        }

        /// <summary>
        /// 判断结果集是否为空
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static Boolean dsIsNull(DataSet ds)
        {
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) return true;
            else return false;
        }

        /// <summary>
        /// 生成新ID
        /// </summary>
        /// <param name="Tab">表名</param>
        /// <param name="Col">字段名</param>
        /// <returns></returns>
        public static int getNewID(string Tab, string Col)
        {

            DataSet ds = ado1.sel1("select Isnull(Max(Convert(int," + Col + ")),0) + 1 as s1 from " + Tab);
            if (dsIsNull(ds)) return 1;
            if (ds.Tables[0].Rows.Count == 1)
                return Convert.ToInt32(ds.Tables[0].Rows[0]["s1"].ToString());
            else
                return 1;
        }

        /// <summary>
        /// 设置字段为主键
        /// </summary>
        /// <param name="sTable">表名</param>
        /// <param name="sField">字段名</param>
        public static void setPrimary(string sTable, string sField)
        {
            try
            {
                String strSQL = "ALTER TABLE [" + sTable + "] ADD  CONSTRAINT [PK_" + sTable + "] PRIMARY KEY CLUSTERED "
                    + "( [" + sField + "] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, "
                    + "IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]";
                ado1.cmd1(strSQL);
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
                //CDebug.Send("设置主键出错", ex);
            }

        }

        public static Boolean isTable(string sTable)
        {
            string strSQL = "select * from sysobjects where id = object_id('" + sTable + "') and type = 'u'";
            DataSet ds1 = ado1.sel1(strSQL);

            if (dsIsNull(ds1)) return false;
            else return true;
        }

        public static Boolean isField(string sTable, string sField)
        {
            string strSQL = "select * from syscolumns where name='" + sField + "' and objectproperty(id,'IsUserTable')=1 "
                + " and object_name(id)='" + sTable + "'";
            DataSet ds1 = ado1.sel1(strSQL);

            if (dsIsNull(ds1)) return false;
            else return true;
        }

        /// <summary>
        /// 处理json中的特殊字符
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static String stringToJson(String s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {

                char c = s[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\"");
                        break;
                    case '\\':   //如果不处理单引号，可以释放此段代码，若结合下面的方法处理单引号就必须注释掉该段代码
                        sb.Append("\\\\");
                        break;
                    case '/':
                        sb.Append("\\/");
                        break;
                    case '\b':      //退格
                        sb.Append("\\b");
                        break;
                    case '\f':      //走纸换页
                        sb.Append("\\f");
                        break;
                    case '\n':
                        sb.Append("\\n");//换行    
                        break;
                    case '\r':      //回车
                        sb.Append("\\r");
                        break;
                    case '\t':      //横向跳格
                        sb.Append("\\t");
                        break;
                    default:
                        sb.Append(c);
                        break;
                }
            }
            return sb.ToString();
        }        
        
        /// <summary>
        /// dataset转json 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DataTable2Json(DataTable dt)
        {
            return DataTable2Json(dt, -1, "'", "", "");
        }

        public static string DataTable2Json(DataTable dt, int i_Count)
        {
            return DataTable2Json(dt, i_Count, "'", "", "");
        }

        public static string DataTable2Json(DataSet ds1, int i_Count, string sSplit, string sNamCount, string sNamRoot)
        {
            return DataTable2Json(ds1.Tables[0], i_Count, sSplit, sNamCount, sNamRoot);
        }

        public static string DataTable2Json(DataTable dt, int i_Count, string sSplit, string sNamCount, string sNamRoot)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                StringBuilder jsonBuilder = new StringBuilder();
                jsonBuilder.Append("{");

                if (i_Count < 0) i_Count = dt.Rows.Count;

                if (sNamCount != string.Empty) jsonBuilder.Append(sSplit + sNamCount + sSplit + ":" + sSplit + i_Count + sSplit + ",");
                else jsonBuilder.Append(sSplit + "totalProperty" + sSplit + ":" + sSplit + i_Count + sSplit + ",");

                if (sNamRoot != string.Empty) jsonBuilder.Append(sSplit + sNamRoot + sSplit);
                else jsonBuilder.Append(sSplit + "ds" + sSplit);

                jsonBuilder.Append(":[");
                if (sSplit == string.Empty) sSplit = "'";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i != 0) jsonBuilder.Append(",");
                    jsonBuilder.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        jsonBuilder.Append(sSplit + dt.Columns[j].ColumnName + sSplit);
                        jsonBuilder.Append(":" + sSplit);
                        jsonBuilder.Append(dt.Rows[i][j].ToString());
                        jsonBuilder.Append(sSplit + ",");
                    }
                    jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                    jsonBuilder.Append("}");
                }
                jsonBuilder.Append("]}");

                return jsonBuilder.ToString();
            }
            else
            {
                return "{ds:{}}";
            }
        }
        /// <summary>
        /// list转json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonName"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string ObjectToJson<T>(string jsonName, IList<T> t)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{\"" + jsonName + "\":[");
            if (t.Count > 0)
            {
                for (int i = 0; i < t.Count; i++)
                {
                    T obj = Activator.CreateInstance<T>();
                    Type type = obj.GetType();
                    PropertyInfo[] pis = type.GetProperties();
                    Json.Append("{");
                    for (int j = 0; j < pis.Length; j++)
                    {
                        Json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(t[i], null) + "\"");
                        if (j < pis.Length - 1)
                        {
                            Json.Append(",");
                        }
                        else
                        {
                            Json.Append("}");
                        }
                        if (i < t.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }



        /// <summary>
        /// 根据字段类型返回字段值
        /// </summary>
        /// <param name="ds1"></param>
        /// <param name="sCol"></param>
        /// <returns></returns>
        public static string getValue(DataSet ds1, string sCol)
        {
            if (dsIsNull(ds1)) return "";
            else
            {
                return ds1.Tables[0].Rows[0][sCol].ToString();
            }
        }

        public static string getValue(DataSet ds1, int iCol)
        {
            if (dsIsNull(ds1)) return "";
            else
            {
                return ds1.Tables[0].Rows[0][iCol].ToString();
            }
        }

        public static string getValue(DataSet ds1, int iRow, string sCol)
        {
            if (dsIsNull(ds1)) return "";
            else
            {
                return ds1.Tables[0].Rows[iRow][sCol].ToString();
            }
        }

        public static string getValue(DataSet ds1, int iRow, int iCol)
        {
            if (dsIsNull(ds1)) return "";
            else
            {
                return ds1.Tables[0].Rows[iRow][iCol].ToString();
            }
        }

        public static double getD(DataSet ds1, int iRow, string sCol)
        {
            try
            {
                string sVal = getValue(ds1, iRow, sCol);
                if (sVal == string.Empty) return 0;
                else return Convert.ToDouble(sVal);
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
                return 0;
            }
        }

        public static double getD(DataSet ds1, int iRow, int iCol)
        {
            try
            {
                return Convert.ToDouble(getValue(ds1, iRow, iCol));
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
                return 0;
            }
        }

        public static double getI(DataSet ds1, int iRow, string sCol)
        {
            return Convert.ToInt32(getValue(ds1, iRow, sCol));
        }

        public static double getI(DataSet ds1, int iRow, int iCol)
        {
            return Convert.ToInt32(getValue(ds1, iRow, iCol));
        }


        /// <summary>
        /// 返回Insert语句
        /// </summary>
        /// <param name="sTabName">Table Name</param>
        /// <param name="dr">DataRow</param>
        /// <param name="b_Col">字段名加[]</param>
        /// <param name="b_Enter">每个字段都换行</param>
        /// <returns></returns>
        public static string getSQL_Insert(string sTabName, DataRow dr, Boolean b_Col, Boolean b_Enter)
        {
            string strSQL_Add = "";
            string sCol = "";
            string sVal = "";
            string sColS = "";
            string sValS = "";
            string s_Enter = "";

            if (b_Enter == true) s_Enter = "\n";

            DataColumnCollection dcs = dr.Table.Columns;
            for (int i = 0; i < dcs.Count; i++)
            {
                if (dr != null && dr[i].ToString() == "") continue;

                DataColumn dc = dcs[i];
                ////CDebug.Send(dc.DataType.Name);
                try
                {
                    if (i > 0)
                    {
                        sColS += ", " + s_Enter;
                        sValS += ", " + s_Enter;
                    }

                    if (b_Col) sCol = "[" + dc.Caption + "]";
                    else sCol = dc.Caption;

                    if (dc.DataType == typeof(Int16) || dc.DataType == typeof(Int32) || dc.DataType == typeof(Int64)
                        || dc.DataType == typeof(float) || dc.DataType == typeof(Double) || dc.DataType == typeof(byte)
                        || dc.DataType == typeof(Boolean))
                    {
                        sVal = dr[i].ToString().Trim();
                        if (sVal == "") sVal = "0";
                    }
                    else if (dc.DataType == typeof(String))
                    {
                        sVal = "'" + dr[i].ToString().Replace("'", "''") + "'";
                    }
                    else
                    {
                        sVal = "'" + dr[i].ToString() + "'";
                    }

                    sColS += sCol;
                    sValS += sVal;
                }
                catch (System.Exception ex)
                {
                    //CDebug.Send(ex.ToString());
                    //CDebug.SendError("向结果集中赋值时出错", ex);
                }
            }

            strSQL_Add = "INSERT INTO " + sTabName + "(" + sColS + ") VALUES (" + sValS + ");" + s_Enter;
            return strSQL_Add;
        }

        /// <summary>
        /// 返回更新语句
        /// </summary>
        /// <returns></returns>
        public static string getSQL_Update(string sTabName, DataRow dr, Boolean b_Col, Boolean b_Enter)
        {
            string s_Enter = "";
            if (b_Enter == true) s_Enter = "\n";

            string strSQL_Edt = "UPDATE " + sTabName + " SET " + s_Enter;
            string sCol = "";
            string sVal = "";
            string strSQL_Key = getSQL_Key(dr);
            DataColumnCollection dcs = dr.Table.Columns;

            for (int i = 0; i < dcs.Count; i++)
            {

                if (i > 0) strSQL_Edt += ", " + s_Enter;

                DataColumn dc = dcs[i];
                if (b_Col) sCol = "[" + dc.Caption + "]";
                else sCol = dc.Caption;

                if (dc.DataType == typeof(Int32))
                {
                    sVal = dr[i].ToString();
                    if (dr[i].ToString() == "")
                        sVal = "0";
                }
                else if (dc.DataType == typeof(String))
                {
                    sVal = "'" + dr[i].ToString().Replace("'", "''") + "'";
                }
                else
                {
                    sVal = "'" + dr[i].ToString() + "'";
                }
                strSQL_Edt += sCol + "=" + sVal;

            }

            strSQL_Edt += s_Enter + " WHERE " + strSQL_Key + ";" + s_Enter;
            return strSQL_Edt;
        }

        /// <summary>
        /// 返回删除语句
        /// </summary>
        /// <returns></returns>
        public static string getSQL_Delete(string sTabName, DataRow dr)
        {
            string strSQL_Key = getSQL_Key(dr);
            string strSQL_Del = "DELETE FROM " + sTabName + " WHERE " + strSQL_Key + ";";
            return strSQL_Del;
        }

        /// <summary>
        /// 返回查询语句
        /// </summary>
        /// <returns></returns>
        public static string getSQL_Select(string sTabName, DataRow dr, Boolean b_Col, Boolean b_Enter)
        {
            string strSQL_Key = getSQL_Key(dr);
            string strSQL_Sel = "SELECT ";
            string sCol = "";
            string s_Enter = "";

            DataColumnCollection dcs = dr.Table.Columns;

            if (b_Enter == true) s_Enter = "\n";

            for (int i = 0; i < dcs.Count; i++)
            {

                if (i > 0) strSQL_Sel += ", " + s_Enter;

                DataColumn dc = dcs[i];

                if (b_Col) sCol = "[" + dc.Caption + "]";
                else sCol = dc.Caption;

                strSQL_Sel += sCol;
            }

            strSQL_Sel += " FROM " + sTabName + s_Enter + " WHERE " + strSQL_Key + ";" + s_Enter;
            return strSQL_Sel;
        }

        public static string getSQL_Key(DataRow dr)
        {
            //CDebug.EnterMethod("加载数据");
            string strSQL_Key = "";

            try
            {
                DataColumnCollection dcs = dr.Table.Columns;
                DataColumn[] dc_Key = dr.Table.PrimaryKey;
                TableKey[] tabKey = new TableKey[dc_Key.Length];

                if (dc_Key.Length > 0)
                {
                    for (int i = 0; i < dc_Key.Length; i++)
                    {
                        tabKey[i].dc = dc_Key[i];

                        for (int k = 0; k < dcs.Count; k++)
                        {
                            if (tabKey[i].dc.Caption == dcs[k].Caption)
                            {
                                tabKey[i].s_Val = dr[k].ToString();
                            }
                        }

                    }
                }
                else
                {

                    tabKey = new TableKey[dcs.Count];
                    for (int i = 0; i < dcs.Count; i++)
                    {
                        tabKey[i].dc = dcs[i];

                        tabKey[i].s_Val = dr[i].ToString();

                    }
                }

                for (int i = 0; i < tabKey.Length; i++)
                {
                    if (i > 0 && strSQL_Key != "") strSQL_Key += " AND ";

                    if (tabKey[i].s_Val != "")
                    {
                        if (tabKey[i].dc.DataType == typeof(Int32))
                            strSQL_Key += tabKey[i].dc.Caption + "=" + tabKey[i].s_Val;
                        else
                            strSQL_Key += tabKey[i].dc.Caption + "='" + tabKey[i].s_Val + "'";
                    }

                }

                if (strSQL_Key == "") strSQL_Key = "1=1";

            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
                //CDebug.Send("生成数据显示界面出错", ex);
            }

            //CDebug.ExitMethod("加载数据");

            return strSQL_Key;
        }
        /*
        /// <summary>
        /// 填充DropDownList(sql中第一个字段为ID, 第二个字段为text)
        /// </summary>
        /// <param name="strSQL">SQL语句（结果集中第一个字段为ID，第二个字段为Text；只有一个字段时，只填充Text）</param>
        /// <param name="dropList"></param>
        public static void fillDrop(string strSQL, ref DropDownList dropList)
        {
            dropList.Items.Clear();
            ////CDebug.Send(strSQL1);
            DataSet ds1 = ado1.sel1(strSQL);
            ////CDebug.Send("返回结果个数：" + ado1.ds_count);
            ////CDebug.Send(ds1);
            if (!CDB.dsIsNull(ds1))
            {
                dropList.DataSource = ds1.Tables[0];
                if (ds1.Tables[0].Columns.Count > 1)
                {
                    dropList.DataValueField = ds1.Tables[0].Columns[0].Caption;
                    dropList.DataTextField = ds1.Tables[0].Columns[1].Caption;
                }
                else
                {
                    dropList.DataTextField = ds1.Tables[0].Columns[0].Caption;
                }

                dropList.DataBind();
            }
        }
        
        /// <summary>
        /// 填充DropDownList
        /// </summary>
        /// <param name="strSQL">SQL语句</param>
        /// <param name="dropList"></param>
        /// <param name="sColID">ID</param>
        /// <param name="sColText">Text</param>
        public static void fillDrop(string strSQL, ref DropDownList dropList, string sColID, string sColText)
        {
            dropList.Items.Clear();
            ////CDebug.Send(strSQL1);
            DataSet ds1 = ado1.sel1(strSQL);
            ////CDebug.Send("返回结果个数：" + ado1.ds_count);
            ////CDebug.Send(ds1);
            if (!CDB.dsIsNull(ds1))
            {
                dropList.DataSource = ds1.Tables[0];
                dropList.DataTextField = sColText;
                dropList.DataValueField = sColID;
                dropList.DataBind();
            }
        }

        public static void fillDrop(DataSet ds1, ref DropDownList dropList)
        {
            dropList.Items.Clear();
            ////CDebug.Send("返回结果个数：" + ado1.ds_count);
            ////CDebug.Send(ds1);
            if (!CDB.dsIsNull(ds1))
            {
                dropList.DataSource = ds1.Tables[0];
                dropList.DataTextField = ds1.Tables[0].Columns[1].Caption;
                dropList.DataValueField = ds1.Tables[0].Columns[0].Caption;
                dropList.DataBind();
            }
        }

        public static void fillDrop(DataSet ds1, ref DropDownList dropList, string sColID, string sColText)
        {
            dropList.Items.Clear();
            ////CDebug.Send("返回结果个数：" + ado1.ds_count);
            ////CDebug.Send(ds1);
            if (!CDB.dsIsNull(ds1))
            {
                dropList.DataSource = ds1.Tables[0];
                dropList.DataTextField = sColText;
                dropList.DataValueField = sColID;
                dropList.DataBind();
            }
        }

        public static void fillDrop_App(DataSet ds1, ref DropDownList dropList, string sColID, string sColText)
        {
            if (!CDB.dsIsNull(ds1))
            {
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    DataRow dr1 = ds1.Tables[0].Rows[i];
                    dropList.Items.Add(new System.Web.UI.WebControls.ListItem(dr1[sColText].ToString(), dr1[sColID].ToString()));
                }
            }
        }
        
        public static void fillComboBox(DataSet ds1, ref ComboBox cbox)
        {
            if (!dsIsNull(ds1))
            {
                DataTable dt = ds1.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    try
                    {
                        DataRow dr = dt.Rows[i];
                        string col1 = dr[0].ToString();
                        string col2 = col1;
                        if (ds1.Tables[0].Columns.Count > 1)
                        {
                            col2 = dr[1].ToString();
                        }

                        cbox.Items.Add(new MyComboBoxItem(col1, col2));
                    }
                    catch (System.Exception ex)
                    {
                        //CDebug.SendError(ex.ToString());
                        //CDebug.Send("填充ComboBox出错", ex);
                    }

                }

            }
        }

        public static void fillComboBox(string strSQL, ref ComboBox cbox)
        {
            try
            {
                DataSet ds1 = ado1.sel1(strSQL);
                fillComboBox(ds1, ref cbox);
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
                //CDebug.Send("填充ComboBox出错", ex);
            }

        }
         * */
        /// <summary>
        /// 结果集创建表
        /// </summary>
        /// <param name="sTab">表名</param>
        /// <param name="ds1"></param>
        /// <param name="dropOldTab"></param>
        public static void creatTabFromDs(string sTab, DataSet ds1, bool dropOldTab)
        {
            if (dropOldTab)
            {
                try
                {
                    ado1.cmd1("DROP TABLE " + sTab);
                }
                catch (System.Exception ex)
                {
                    //CDebug.SendError(ex.ToString());
                }
            }

            string strSQL = "";
            for (int i = 0; i < ds1.Tables[0].Columns.Count; i++)
            {
                string sCol = ds1.Tables[0].Columns[i].Caption;
                if (i == 0)
                    strSQL = "CREATE TABLE " + sTab + " ([" + sCol + "] [varchar](50) NOT NULL";
                else
                    strSQL += ",[" + sCol + "] [varchar](50) NULL";
            }
            strSQL += ") ON [PRIMARY]";
            ado1.cmd1(strSQL);
        }


        /// <summary>
        /// 结果集存入数据库
        /// </summary>
        /// <param name="ds1"></param>
        /// <param name="sTab">表名</param>
        public static void dsToDB(DataSet ds1, string sTab)
        {
            if (dsIsNull(ds1)) return;

            try
            {
                string strSQL = "";

                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    DataRow dr_Old = ds1.Tables[0].Rows[i];
                    for (int j = 0; j < ds1.Tables[0].Columns.Count; j++)
                    {
                        if (j == 0) strSQL = "'" + dr_Old[j].ToString() + "'";
                        else strSQL += ",'" + dr_Old[j].ToString() + "'";
                    }

                    strSQL = "INSERT INTO [" + sTab + "] VALUES (" + strSQL + ")";
                    ado1.cmd1(strSQL);
                }
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
                //CDebug.Send("保存结果集到数据库时出错", ex);
            }

        }

        /// <summary>
        /// 同dsToDB(DataSet ds1, string sTab)
        /// </summary>
        /// <param name="ds1"></param>
        /// <param name="sTab"></param>
        public static void dsToTable(DataSet ds1, string sTab)
        {
            if (dsIsNull(ds1)) return;

            try
            {
                string strSQL = "";

                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    DataRow dr_Old = ds1.Tables[0].Rows[i];
                    for (int j = 0; j < ds1.Tables[0].Columns.Count; j++)
                    {
                        if (j == 0) strSQL = "'" + dr_Old[j].ToString() + "'";
                        else strSQL += ",'" + dr_Old[j].ToString() + "'";
                    }

                    strSQL = "INSERT INTO [" + sTab + "] VALUES (" + strSQL + ")";
                    ado1.cmd1(strSQL);
                }
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
                //CDebug.Send("保存结果集到数据库时出错", ex);
            }
        }

        /// <summary>
        /// 将数据插入表中
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="sTab"></param>
        public static void insertToTable(List<object> obj, string sTab)
        {
            string strSQL = "";
            for (int i = 0; i < obj.Count; i++)
            {
                if (obj[i] is string)
                {
                    if (strSQL == string.Empty)
                        strSQL += "'" + obj[i].ToString() + "'";
                    else
                        strSQL += ",'" + obj[i].ToString() + "'";
                }

                if (obj[i] is int)
                {
                    if (strSQL == string.Empty)
                        strSQL += obj[i].ToString();
                    else
                        strSQL += "," + obj[i].ToString();
                }
            }

            strSQL = "INSERT INTO " + sTab + " VALUES(" + strSQL + ")";
            ado1.cmd1(strSQL);
        }

        public static void deleteFromTable(string sField, object o_Val, string sTab)
        {
            string strSQL = "";
            if (o_Val is string)
                strSQL = "'" + o_Val.ToString() + "'";

            if (o_Val is int)
                strSQL = o_Val.ToString();

            strSQL = "DELETE FROM " + sTab + " WHERE " + sField + "=" + strSQL;
            ado1.cmd1(strSQL);
        }

        /// <summary>
        /// 查询表中的数据，返回结果集
        /// </summary>
        /// <param name="sTab"></param>
        /// <returns>结果集</returns>
        public static DataSet selectFromTable(string sTab)
        {
            return ado1.sel1("SELECT * FROM " + sTab);
        }

        public static DataSet selectFromTable(string sTab, string sWhere)
        {
            return ado1.sel1("SELECT * FROM " + sTab + " WHERE " + sWhere);
        }


        /// <summary>
        /// 生成html表格
        /// </summary>
        /// <param name="ds1"></param>
        /// <param name="sClass"></param>
        /// <returns></returns>
        public static string dsToHtmlTable(DataSet ds1, string sClass)
        {
            return dsToHtmlTable(ds1, sClass, null, null);
        }

        public static string dsToHtmlTable(DataSet ds1)
        {
            return dsToHtmlTable(ds1, "");
        }

        /// <summary>
        /// 动态生成表格
        /// </summary>
        /// <param name="ds1">数据集</param>
        /// <param name="sClass">表格样式及属性</param>
        /// <param name="h_W">表头每列宽度</param>
        /// <returns></returns>
        public static string dsToHtmlTable(DataSet ds1, string sClass, int[] h_W)
        {
            StringBuilder sTab = new StringBuilder();
            sTab.Append("<table " + sClass + "><tr>");
            //表头
            if (ds1 == null || ds1.Tables.Count == 0) return "";

            for (int i = 0; i < ds1.Tables[0].Columns.Count; i++)
            {
                string s_style = "";
                if (ds1.Tables[0].Columns[i].Caption != "color")
                {
                    if (i <= h_W.Length - 1)
                        s_style = "style='width:" + h_W[i] + "px; height:25px'";

                    sTab.Append("<th " + s_style + ">" + ds1.Tables[0].Columns[i].Caption + "</th>");
                }
            }
            sTab.Append("</tr>");

            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                DataRow dr1 = ds1.Tables[0].Rows[i];
                sTab.Append("<tr>");
                for (int j = 0; j < ds1.Tables[0].Columns.Count; j++)
                {
                    ////CDebug.Send(ds1.Tables[0].Columns[j].DataType.ToString());
                    if (ds1.Tables[0].Columns[j].Caption != "color")
                    {
                        if (ds1.Tables[0].Columns[j].DataType == System.Type.GetType("System.Decimal"))
                        {
                            sTab.Append("<td style='height:25px;background-color:" + dr1["color"].ToString() + "'>" + Convert.ToDouble(dr1[j]).ToString() + "</td>");
                        }
                        else if (ds1.Tables[0].Columns[j].DataType == System.Type.GetType("System.String"))
                        {
                            sTab.Append("<td style='height:25px;background-color:" + dr1["color"].ToString() + "'>" + dr1[j].ToString() + "</td>");
                        }
                        else
                        {
                            sTab.Append("<td style='height:25px;  background-color:" + dr1["color"].ToString() + "'>" + dr1[j].ToString() + "</td>");
                        }
                    }
                }
                sTab.Append("</tr>");
            }
            sTab.Append("</table>");
            return sTab.ToString();
        }

        /// <summary>
        /// 动态生成表格
        /// </summary>
        /// <param name="ds1">数据集</param>
        /// <param name="sClass">表格样式及属性</param>
        /// <param name="th_style">表头每列样式</param>
        /// <param name="td_style">正文每列样式</param>
        /// <returns></returns>
        public static string dsToHtmlTable(DataSet ds1, string sClass, string[] th_style, string[] td_style)
        {
            StringBuilder sTab = new StringBuilder();
            sTab.Append("<table " + sClass + "><tr>");
            //表头
            if (ds1 == null || ds1.Tables.Count == 0) return "";

            for (int i = 0; i < ds1.Tables[0].Columns.Count; i++)
            {
                string s_style = " class='fixedtd'   ";
                if (th_style != null && i <= th_style.Length - 1) s_style = th_style[i];

                sTab.Append("<th " + s_style + ">" + ds1.Tables[0].Columns[i].Caption + "</th>");
            }
            sTab.Append("</tr>");

            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                DataRow dr1 = ds1.Tables[0].Rows[i];
                sTab.Append("<tr>");
                for (int j = 0; j < ds1.Tables[0].Columns.Count; j++)
                {
                    string s_style = "";
                    if (td_style != null && j <= td_style.Length - 1) s_style = td_style[j];

                    ////CDebug.Send(ds1.Tables[0].Columns[j].DataType.ToString());
                    if (ds1.Tables[0].Columns[j].Caption != "color")
                    {
                        if (ds1.Tables[0].Columns[j].DataType == System.Type.GetType("System.Decimal"))
                        {
                            if (dr1[j].ToString().Contains("9999"))
                            {
                                sTab.Append("<td " + s_style + "></td>");
                            }
                            else
                            {

                                sTab.Append("<td " + s_style + ">" + Convert.ToDouble(dr1[j]).ToString() + "</td>");
                            }
                        }
                        else if (ds1.Tables[0].Columns[j].DataType == System.Type.GetType("System.String"))
                        {
                            sTab.Append("<td " + s_style + ">" + dr1[j].ToString() + "</td>");
                        }
                        else if (ds1.Tables[0].Columns[j].DataType == System.Type.GetType("System.DateTime"))
                        {
                            if (Convert.ToDateTime(dr1[j]).ToString("HHmmss") == "000000")
                            {
                                sTab.Append("<td " + s_style + ">" + Convert.ToDateTime(dr1[j]).ToString("yyyy-MM-dd") + "</td>");
                            }
                            else
                            {
                                sTab.Append("<td " + s_style + ">" + dr1[j].ToString() + "</td>");
                            }
                        }
                        else
                        {
                            sTab.Append("<td " + s_style + ">" + dr1[j].ToString() + "</td>");
                        }


                    }
                }
                sTab.Append("</tr>");
            }
            sTab.Append("</table>");
            return sTab.ToString();
        }

        /// <summary>
        /// 动态生成表格
        /// </summary>
        /// <param name="ds1">数据集</param>
        /// <param name="sClass">表格样式及属性</param>
        /// <param name="th_style">表头每列样式</param>
        /// <param name="td_style">正文每列样式</param>
        /// <param name="col_Name">要显示的字段名称(没有则为空)</param>
        /// <returns></returns>
        public static string dsToHtmlTable(DataSet ds1, string sClass, string[] th_style, string[] td_style, string[] col_Name)
        {
            StringBuilder sTab = new StringBuilder();
            sTab.Append("<table " + sClass + "><tr>");
            //表头
            if (ds1 == null || ds1.Tables.Count == 0) return "";


            if (col_Name != null)
            {
                //指定表头
                for (int i = 0; i < col_Name.Length; i++)
                {
                    string s_style = "";
                    if (th_style != null && i <= th_style.Length - 1) s_style = th_style[i];

                    sTab.Append("<th " + s_style + ">" + col_Name[i] + "</th>");
                }
            }
            else
            {
                //动态表头
                for (int i = 0; i < ds1.Tables[0].Columns.Count; i++)
                {
                    string s_style = "";
                    if (th_style != null && i <= th_style.Length - 1) s_style = th_style[i];

                    sTab.Append("<th " + s_style + ">" + ds1.Tables[0].Columns[i].Caption + "</th>");
                }
            }

            sTab.Append("</tr>");
            //数据
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                DataRow dr1 = ds1.Tables[0].Rows[i];
                sTab.Append("<tr>");

                if (col_Name != null)
                {
                    //指定字段
                    for (int j = 0; j < col_Name.Length; j++)
                    {
                        string s_style = "";
                        if (td_style != null && j <= td_style.Length - 1) s_style = td_style[j];

                        ////CDebug.Send(ds1.Tables[0].Columns[j].DataType.ToString());
                        string s_col = col_Name[j];

                        if (ds1.Tables[0].Columns[j].DataType == System.Type.GetType("System.Decimal"))
                        {
                            sTab.Append("<td " + s_style + ">" + Convert.ToDouble(dr1[s_col]).ToString() + "</td>");
                        }
                        else if (ds1.Tables[0].Columns[j].DataType == System.Type.GetType("System.String"))
                        {
                            sTab.Append("<td " + s_style + ">" + dr1[s_col].ToString() + "</td>");
                        }
                        else
                        {
                            sTab.Append("<td " + s_style + ">" + dr1[s_col].ToString() + "</td>");
                        }
                    }
                }
                else
                {
                    //所有字段
                    for (int j = 0; j < ds1.Tables[0].Columns.Count; j++)
                    {
                        string s_style = "";
                        if (td_style != null && j <= td_style.Length - 1) s_style = td_style[j];

                        ////CDebug.Send(ds1.Tables[0].Columns[j].DataType.ToString());
                        if (ds1.Tables[0].Columns[j].Caption != "color")
                        {
                            if (ds1.Tables[0].Columns[j].DataType == System.Type.GetType("System.Decimal"))
                            {
                                sTab.Append("<td " + s_style + ">" + Convert.ToDouble(dr1[j]).ToString() + "</td>");
                            }
                            else if (ds1.Tables[0].Columns[j].DataType == System.Type.GetType("System.String"))
                            {
                                sTab.Append("<td " + s_style + ">" + dr1[j].ToString() + "</td>");
                            }
                            else
                            {
                                sTab.Append("<td " + s_style + ">" + dr1[j].ToString() + "</td>");
                            }
                        }
                    }
                }

                sTab.Append("</tr>");
            }
            sTab.Append("</table>");
            return sTab.ToString();
        }


        /// <summary>
        /// 多行数据转为多列表格
        /// </summary>
        /// <param name="ds1">结果集</param>
        /// <param name="sClass">HTML表格样式</param>
        /// <param name="sCol">字段名列表(不指定时默认为前两列)</param>
        /// <param name="th_style">表头样式</param>
        /// <param name="td_style">正文样式</param>
        /// <param name="iCol_Count">转换后的表格列数</param>
        /// <returns></returns>
        public static string dsToHtmlTable_ColToRow(DataSet ds1, string sClass, string[] sCol, string th_style, string td_style, int iCol_Count)
        {
            string sTab = "<table " + sClass + "><tr>";
            //表头
            if (ds1.Tables.Count == 0) return "";

            string sColHead = "";
            string sColBody = "";

            if (sCol == null || sCol.Length == 0)
            {
                sColHead = ds1.Tables[0].Columns[0].Caption;
                sColBody = ds1.Tables[0].Columns[1].Caption;
            }
            else if (sCol.Length >= 2)
            {
                sColHead = sCol[0];
                sColBody = sCol[1];
            }
            else if (sCol.Length == 1)
            {
                sColHead = sCol[0];
                sColBody = sCol[1];
            }

            //动态表头
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                sTab += "<th " + th_style + ">" + ds1.Tables[0].Rows[i][0].ToString() + "</th>";
                string sVal = "";
                sVal = ds1.Tables[0].Rows[i][1].ToString();
                sTab += "<td " + td_style + ">" + sVal + "</td>";

                if ((i + 1) % iCol_Count == 0)
                {
                    sTab += "</tr><tr>";
                }
            }

            //空位补全
            if (ds1.Tables[0].Rows.Count % iCol_Count != 0)
            {
                int i_Empty = iCol_Count - (ds1.Tables[0].Rows.Count % iCol_Count);
                for (int i = 0; i < i_Empty; i++)
                {
                    sTab += "<th " + th_style + ">" + "" + "</th>";
                    sTab += "<td " + td_style + ">" + "" + "</td>";
                }
            }

            sTab += "</tr>";

            sTab += "</table>";
            return sTab;
        }

        /// <summary>
        /// 多行数据转为多列表格
        /// </summary>
        /// <param name="ds1">结果集</param>
        /// <param name="sClass">HTML表格样式</param>
        /// <param name="sCol">字段名列表(不指定时默认为前两列)</param>
        /// <param name="fixCol">固定列数量</param>
        /// <param name="th_style">表头样式</param>
        /// <param name="td_style">正文样式</param>
        /// <param name="iCol_Count">转换后的表格列数</param>
        /// <returns></returns>
        public static string dsToHtmlTable_ColToRow(DataSet ds1, string sClass, string[] sCol, int fixCol, string th_style, string td_style, int iCol_Count)
        {
            StringBuilder sTab = new StringBuilder();
            sTab.Append("<table " + sClass + ">");
            sTab.Append("<tr>");
            //表头
            if (ds1.Tables.Count == 0) return "";

            DataColumnCollection dc = ds1.Tables[0].Columns;

            //动态表头
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                DataRow dr1 = ds1.Tables[0].Rows[i];
                if (sCol != null && sCol.Length > 0)
                {
                    //根据字段名填充
                    for (int j = 0; j < sCol.Length; j++)
                    {
                        if (j < fixCol) sTab.Append("<th " + th_style + ">" + dr1[sCol[j]].ToString() + "</th>");   //列头
                        else sTab.Append("<td " + td_style + ">" + dr1[sCol[j]].ToString() + "</td>");              //数据
                    }
                }
                else
                {
                    //根据序号填充数据
                    for (int j = 0; j < dc.Count; j++)
                    {
                        if (j < fixCol) sTab.Append("<th " + th_style + ">" + dr1[j].ToString() + "</th>");         //列头
                        else sTab.Append("<td " + td_style + ">" + dr1[j].ToString() + "</td>");                    //数据

                    }
                }
                if ((i + 1) % iCol_Count == 0) sTab.Append("</tr><tr>");                                            //换行
            }

            //空位补全
            if (ds1.Tables[0].Rows.Count % iCol_Count != 0)
            {
                int i_Empty = iCol_Count - (ds1.Tables[0].Rows.Count % iCol_Count);
                for (int i = 0; i < i_Empty; i++)
                {
                    if (sCol != null && sCol.Length > 0)
                    {
                        for (int j = 0; j < sCol.Length; j++)
                        {
                            if (j < fixCol) sTab.Append("<th " + th_style + "></th>");
                            else sTab.Append("<td " + td_style + "></td>");
                        }
                    }
                    else
                    {
                        for (int j = 0; j < dc.Count; j++)
                        {
                            if (j < fixCol) sTab.Append("<th " + th_style + "></th>");
                            else sTab.Append("<td " + td_style + "></td>");
                        }
                    }
                }
            }

            sTab.Append("</tr>");

            sTab.Append("</table>");
            return sTab.ToString();
        }

        /// <summary>
        /// 单行数据转为多列表格
        /// </summary>
        /// <param name="dr1"></param>
        /// <param name="sClass"></param>
        /// <param name="th_style"></param>
        /// <param name="td_style"></param>
        /// <param name="iCol_Count"></param>
        /// <returns></returns>
        public static string drToHtmlTable(DataRow dr1, string sClass, string th_style, string td_style, int iCol_Count)
        {
            string sTab = "<table " + sClass + "><tr>";
            //表头
            if (dr1 == null || dr1.Table.Columns.Count == 0) return "";

            //动态表头
            for (int i = 0; i < dr1.Table.Columns.Count; i++)
            {
                sTab += "<th " + th_style + ">" + dr1.Table.Columns[i].Caption + "</th>";
                sTab += "<td " + td_style + ">" + dr1[i].ToString() + "</td>";

                if ((i + 1) % iCol_Count == 0)
                {
                    sTab += "</tr><tr>";
                }
            }

            if (dr1.Table.Columns.Count % iCol_Count != 0)
            {
                int i_Empty = iCol_Count - (dr1.Table.Columns.Count % iCol_Count);
                for (int i = 0; i < i_Empty; i++)
                {
                    sTab += "<th " + th_style + ">" + "" + "</th>";
                    sTab += "<td " + td_style + ">" + "" + "</td>";
                }
            }

            sTab += "</tr>";

            sTab += "</table>";
            return sTab;
        }

        /// <summary>
        /// 单行数据转为多列表格
        /// </summary>
        /// <param name="ds1"></param>
        /// <param name="i_Row"></param>
        /// <param name="sClass"></param>
        /// <param name="th_style"></param>
        /// <param name="td_style"></param>
        /// <param name="iCol_Count"></param>
        /// <returns></returns>
        public static string drToHtmlTable(DataSet ds1, int i_Row, string sClass, string th_style, string td_style, int iCol_Count)
        {
            string sTab = "<table " + sClass + "><tr>";
            //表头
            if (ds1.Tables.Count == 0) return "";

            //动态表头
            for (int i = 0; i < ds1.Tables[0].Columns.Count; i++)
            {
                sTab += "<th " + th_style + ">" + ds1.Tables[0].Columns[i].Caption + "</th>";
                string sVal = "";
                if (ds1.Tables[0].Rows.Count > i_Row) sVal = ds1.Tables[0].Rows[i_Row][i].ToString();
                sTab += "<td " + td_style + ">" + sVal + "</td>";

                if ((i + 1) % iCol_Count == 0)
                {
                    sTab += "</tr><tr>";
                }
            }

            if (ds1.Tables[0].Columns.Count % iCol_Count != 0)
            {
                int i_Empty = iCol_Count - (ds1.Tables[0].Columns.Count % iCol_Count);
                for (int i = 0; i < i_Empty; i++)
                {
                    sTab += "<th " + th_style + ">" + "" + "</th>";
                    sTab += "<td " + td_style + ">" + "" + "</td>";
                }
            }

            sTab += "</tr>";

            sTab += "</table>";
            return sTab;
        }
        /*
        /// <summary>
        /// ds生成xls
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="SaveFile"></param>
        public static void DsToExcel(DataSet ds, string SaveFile)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application excel;
                Microsoft.Office.Interop.Excel._Workbook xbk;
                Microsoft.Office.Interop.Excel._Worksheet xst;
                object misValue = System.Reflection.Missing.Value;

                excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                xbk = excel.Workbooks.Add(misValue);
                xst = (Microsoft.Office.Interop.Excel._Worksheet)xbk.ActiveSheet;

                int rowIndex = 1;
                int colIndex = 0;

                //取得标题
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    colIndex++;
                    excel.Cells[1, colIndex] = col.ColumnName;
                }

                //取得表格中的数据
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    rowIndex++;
                    colIndex = 0;
                    foreach (DataColumn col in ds.Tables[0].Columns)
                    {
                        colIndex++;
                        excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
                        //设置表格内容居中对齐
                        xst.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
                    }
                }

                excel.Visible = false;
                xbk.SaveAs(SaveFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

                ds = null;
                xbk.Close(true, misValue, misValue);
                excel.Quit();
                PublicMethod.Kill(excel);//调用kill当前excel进程

                releaseObject(xst);
                releaseObject(xbk);
                releaseObject(excel);
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }
        

        }
        */
        public static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                //CDebug.SendError(ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        /// <summary>
        /// dataset to json 用于combobox等
        /// </summary>
        /// <param name="ds1"></param>
        /// <returns></returns>
        public static string dsToJson(DataSet ds1)
        {

            StringBuilder sJson = new StringBuilder();

            try
            {

                if (!CDB.dsIsNull(ds1))
                {
                    DataColumnCollection dc1 = ds1.Tables[0].Columns;
                    sJson.Append("[");

                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        if (i != 0) sJson.Append(",");
                        sJson.Append("{");
                        for (int j = 0; j < ds1.Tables[0].Columns.Count; j++)
                        {
                            if (j != 0) sJson.Append(",");
                            sJson.Append("\"" + dc1[j].Caption.ToLower() + "\":\"" + dr1[j].ToString() + "\"");
                        }
                        sJson.Append("}");
                    }

                    sJson.Append("]");
                }
                else
                {
                    sJson.Append("[]");
                }
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }

            return sJson.ToString();

        }

        /// <summary>
        /// dataset to json 用户 datagrid
        /// </summary>
        /// <param name="ds1"></param>
        /// <param name="i_Page"></param>
        /// <param name="i_Rows"></param>
        /// <returns></returns>
        public static string  dsToJson(DataSet ds1,int i_Page,int i_Rows)
        {

            StringBuilder sJson = new StringBuilder();

            try
            {

                if (!CDB.dsIsNull(ds1))
                {
                    DataColumnCollection dc1 = ds1.Tables[0].Columns;
                    sJson.Append("{\"total\":" + ds1.Tables[0].Rows.Count + ",");
                    sJson.Append("\"rows\":[");

                    //删除分页之外的多余数据
                    int i_Count = ds1.Tables[0].Rows.Count;
                    if (i_Page > 0 && i_Rows > 0 && !CDB.dsIsNull(ds1) && i_Count > (i_Page - 1) * i_Rows)
                    {                        
                        for (int i = i_Count - 1; i >= i_Page * i_Rows; i--) ds1.Tables[0].Rows.RemoveAt(i);
                        for (int i = (i_Page - 1) * i_Rows - 1; i >= 0; i--) ds1.Tables[0].Rows.RemoveAt(i);
                    }

                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        if (i != 0) sJson.Append(",");
                        sJson.Append("{");
                        for (int j = 0; j < ds1.Tables[0].Columns.Count; j++)
                        {
                            if (j != 0) sJson.Append(",");
                            sJson.Append("\"" + dc1[j].Caption.ToLower() + "\":\"" + dr1[j].ToString() + "\"");
                        }
                        sJson.Append("}");
                    }

                    sJson.Append("]}");
                }
                else
                {
                    sJson.Append("{\"total\":" + 0 + ",");
                    sJson.Append("\"rows\":[");
                    sJson.Append("]}");
                }
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }

            return sJson.ToString();

        }
        /*
        /// <summary>
        /// json序列化（非二进制方式）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string JsonSerializer<T>(T t)
        {
            JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
            return jsonSerialize.Serialize(t);
        }

        /// <summary>
        /// json反序列化（非二进制方式）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public static T JsonDeserialize<T>(string jsonString)
        {
            JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
            return (T)jsonSerialize.Deserialize<T>(jsonString);
        }
        */

        /// <summary>
        /// JSON序列化(二进制方式，实体类使用[Serializable])
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string JsonSerializerIO<T>(T t)
        {
#if NET4
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream())
            {
                ser.WriteObject(ms, t);
                string jsonString = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return jsonString;
            }
#endif
            return "";
        }

        /// <summary>
        /// JSON反序列化(二进制方法，实体类使用[Serializable])
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public static T JsonDeserializeIO<T>(string jsonString)
        {
#if NET4
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
            {
                T obj = (T)ser.ReadObject(ms);
                return obj;
            }
#endif
            return default(T);
        }
 
    }
    /*
    public class PublicMethod
    {
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

        public static void Kill(Microsoft.Office.Interop.Excel.Application excel)
        {
            try
            {
                IntPtr t = new IntPtr(excel.Hwnd);   //得到这个句柄，具体作用是得到这块内存入口 
                int k = 0;
                GetWindowThreadProcessId(t, out k);   //得到本进程唯一标志k
                System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);   //得到对进程k的引用
                p.Kill();     //关闭进程k
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }

        }
    }
    */
}
